<?php

	/**
	 * @see Bike_Model_Abstract
	 */
	require_once 'Bike/Model/Abstract.php';
	
	/**
	 * @see Bike_String
	 */
	require_once 'Bike/String.php';
	
	/**
	 * @see Zend_Config
	 */
	require_once 'Zend/Config.php';
	
	/**
	 * @see Zend_Application_Module_Autoloader
	 */
	require_once 'Zend/Application/Module/Autoloader.php';
	
	/**
	 * @see Zend_Controller_Router_Route
	 */
	require_once 'Zend/Controller/Router/Route.php';
	
	
	class Bike_Model_Module extends Bike_Model_Abstract
	{
		protected $_config;

		public function getModuleName()
		{			return Bike_String::factory($this->getName())->toLinknameFromCamelCase();
		}

		public function getModuleDirectory()
		{			return $this->getFrontController()->getModulesDirectory().DIRECTORY_SEPARATOR.$this->getFolderName();
		}

		public function getControllerDirectory()
		{
			return $this->getModuleDirectory().DIRECTORY_SEPARATOR.$this->getFrontController()->getModuleControllerDirectoryName();
		}

		public function setAutoloader()
		{			$loader = new Zend_Application_Module_Autoloader(array(
					'namespace' => $this->getNamespace(),
					'basePath'  => $this->getModuleDirectory(),
			));
			return $loader;
		}

		protected function getConfigFilePath()
        {
        	return $this->getModuleDirectory().DIRECTORY_SEPARATOR.$this->getConfigFileName();
        }

        protected function getConfigFileType()
        {
        	$matches = array();
        	preg_match("/\.([\w\d]{2,4})$/s", $this->getConfigFileName(), $matches);
        	return $matches[1];
        }

        public function getConfig()
        {        	if(is_null($this->_config))
			{
				$fileType = $this->getConfigFileType();
				$configClassName = 'Zend_Config_'.Bike_String::factory($fileType)->toCamelCase();
				if(@class_exists($configClassName))
				{
					$this->_config = new $configClassName($this->getConfigFilePath(), APPLICATION_ENV);
				}
				else
				{					require_once 'Zend/Config/Exception.php';
					throw new Zend_Config_Exception('Undefined config file type \''.$fileType.'\'');
				}
			}
			return $this->_config;
        }

        public function addRoutes()
        {        	$routes = $this->getConfig()->module->routes;
        	if($routes instanceof Zend_Config)
        	{
	        	$routes->rewind();
	        	while($routes->valid())
	        	{	        		$route = $routes->current();
	        		$this->getFrontController()->getRouter()->addRoute(
					    $routes->key(),
					    new Zend_Controller_Router_Route($route->route,
					                                     array('module' => $this->getModuleName(),
					                                     	   'controller' => $route->controller,
					                                           'action' => $route->action))
					);
	        		$routes->next();
	        	}
	      	}
        }
        
        public function getMenuAdmin()
        {
			require_once 'Bike/Model/Mapper/MenuAdmin.php';
        	$mapper = Bike_Model_Mapper_MenuAdmin::getInstance();
			print_r($mapper);        	
        }
	}